package com.wang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.entity.TbBbsComment;
import com.wang.mapper.TbBbsCommentMapper;
import com.wang.response.ResultResponse;
import com.wang.service.TbBbsCommentService;
import com.wang.unitl.SensitiveWordInit;
import com.wang.unitl.SensitivewordFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 评论
 */
@Service
public class TbBbsCommentServiceImpl implements TbBbsCommentService{

    @Autowired
    private TbBbsCommentMapper tbBbsCommentMapper;


    /*
     添加评论并测试敏感词
     */
    @Override
    public ResultResponse insertbbscomment(TbBbsComment tbBbsComment) throws Exception {
        List<String> datas= SensitiveWordInit.readSensitiveWordFile();
        //初始化词库,读取敏感词库，将敏感词放入HashSet中，构建一个DFA算法模型：
        SensitiveWordInit.init(datas);
        //输入的字符串
        String rep = SensitivewordFilter.replaceSensitiveWord(datas,tbBbsComment.getCommentContent(),1,"*");
        System.out.println(rep);
        tbBbsComment.setCommentContent(rep);
        int insert = tbBbsCommentMapper.insert(tbBbsComment);
        if (insert>0){
            return ResultResponse.SUCCESS();
        }else{
            return  ResultResponse.FAIED(500,"添加失败");
        }
    }

    @Override
    public ResultResponse removecomment(Integer commentId) {
        int i = tbBbsCommentMapper.deleteById(commentId);
        if (i>0){
            return ResultResponse.SUCCESS();
        }else{
            return  ResultResponse.FAIED(500,"添加失败");
        }
    }

    @Override
    public ResultResponse findcomment(TbBbsComment tbBbsComment) {
        QueryWrapper<TbBbsComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("father_id", 0);
        List<TbBbsComment> bbsComments = tbBbsCommentMapper.selectList(queryWrapper);
        return ResultResponse.SUCCESS(bbsComments);
    }

    /*
       添加 评论回复
     */
    @Override
    public ResultResponse insertcommentreply(TbBbsComment tbBbsComment) throws Exception {
        List<String> datas= SensitiveWordInit.readSensitiveWordFile();
        //初始化词库,读取敏感词库，将敏感词放入HashSet中，构建一个DFA算法模型：
        SensitiveWordInit.init(datas);
        //输入的字符串
        String rep = SensitivewordFilter.replaceSensitiveWord(datas,tbBbsComment.getCommentContent(),1,"*");
        tbBbsComment.setCommentContent(rep);
        System.out.println(rep);
        int insert = tbBbsCommentMapper.insert(tbBbsComment);
        if (insert>0){
            return ResultResponse.SUCCESS();
        }else{
            return  ResultResponse.FAIED(500,"添加失败");
        }
    }


    /*
    查看评论回复
  */
    @Override
    public ResultResponse findcommentreply(TbBbsComment tbBbsComment) {
        QueryWrapper<TbBbsComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("father_id", tbBbsComment.getCommentId());
        List<TbBbsComment> bbsComments = tbBbsCommentMapper.selectList(queryWrapper);
        return ResultResponse.SUCCESS(bbsComments);
    }

      /*
      查看所有评论
       */
    @Override
    public ResultResponse findcommentall(TbBbsComment tbBbsComment) {
        ArrayList list = new ArrayList();
        Page<TbBbsComment> page = new Page<>(tbBbsComment.getPagenum(), tbBbsComment.getPagesize());
        Page<TbBbsComment> page1 = tbBbsCommentMapper.selectPage(page, null);
        long total = page1.getTotal();
        return ResultResponse.SUCCESS(page1.getRecords(),total);
    }
}




